# -*- coding:utf-8 -*-

import cqplus

import helper
import jx3_com
import options
import proc_msg
import sign_helper
import win_msgbox
import win_send_group
import win_send_private
import win_settings


class MainHandler(cqplus.CQPlusHandler):
	def handle_event(self, event, params):
		
		if event == 'on_enable':
			self.on_enable()
		elif event == 'on_timer':
			# self.logging.debug(params)
			self.on_timer(params)
		elif event == 'on_menu':
			# self.logging.debug(params)
			self.on_menu(params)
		
		elif event=='on_group_member_increase':
			self.on_group_member_increase(params)
		elif event=='on_group_member_decrease':
			self.on_group_member_decrease(params)
		elif event == 'on_private_msg':
			self.on_private_msg(params)
		elif event == 'on_group_msg':
			self.on_group_msg(params)
	
	def on_enable(self):
		work_dir = helper.get_work_dir()
		helper.chk_dir(work_dir)
		helper.init_group_options(self.api.get_group_list())
        helper.login_userid = self.api.get_login_user_id()
        self.logging.debug('check ' + str(helper.login_userid))
	
	def on_private_msg(self, params):
		self.logging.debug(params['from_qq'])
		# self.api.send_private_msg(params['from_qq'],'我只在群里说话')
        msg = proc_msg.proc_private_msg(self, params)
        if msg is not None:
            self.api.send_private_msg(params['from_qq'], msg)
	
	def on_group_msg(self, params):
		group_id = params['from_group']
		msg = proc_msg.proc_group_msg(self, params)
		if msg is not None:
			self.api.send_group_msg(group_id, msg)
	
	def on_menu(self, params):
		self.logging.debug(params['name'])
		menu_id = params['name']
		
		if menu_id == 'menu01':
			recv_str = jx3_com.get_cookie()
			win_msgbox.show(recv_str)
		
		elif menu_id == 'menu02':
			win_settings.show()
		
		elif menu_id == 'menu03':
            win_send_group.show(self)

        elif menu_id == 'menu04':
            win_send_private.show(self)
			
		else:
			self.logging.debug('menu: ' + params['setting'])
	
	def on_timer(self, params):
		self.logging.debug(params['name'])
		timer_id = params['name']

        if timer_id == 'timer01' and not helper.is_silent():
            self.logging.debug(str(options.GROUP_OPTIONS))
			group_send = helper.get_board_qiyu_groupids()
			self.logging.debug('send:' + str(group_send))
			if len(group_send) > 0:
				qiyu = jx3_com.search_new_qiyu()
				if qiyu == '1':  # 提醒登录
					self.api.send_private_msg(12066230, '查询授权过期，需要登录')
				self.logging.debug('new:' + qiyu)
                if len(qiyu) > 20:
					for gp in group_send:
						self.api.send_group_msg(gp, qiyu)

        elif timer_id == 'timer02' and not helper.is_silent():
            group_send = helper.get_board_server_groupids()
            self.logging.debug('send:' + str(group_send))
            if len(group_send) > 0:
                server_status = jx3_com.get_server_status_timer()
                if server_status is not None and len(server_status) > 10:
                    for gp in group_send:
                        self.api.send_group_msg(gp, server_status)

	def on_group_member_increase(self,params):
		group_id = params['from_group']
		user_id=params['being_operate_qq']
		group_info=self.api.get_group_member_list(group_id)
        msg = sign_helper.get_welcome_msg(group_info, user_id)
		if msg is not None:
            msg = helper.str_at(user_id) + '\r\n' + msg
			self.api.send_group_msg(group_id, msg)
			
	def on_group_member_decrease(self,params):
		group_id = params['from_group']
		user_id=params['being_operate_qq']
		op_user=params.get('from_qq')
		# group_info=self.api.get_group_member_list(group_id)
        user_info = self.api.get_stranger_info(user_id, True)
        msg = sign_helper.get_bye_msg(user_info, op_user)
		if msg is not None:
			self.api.send_group_msg(group_id, msg)
		
		